package com.zkdbchange.dbchange.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSON;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class ChangeDataSource {
    //链接ZK从zk获取配置

    private static CuratorFramework curatorFramework;

    static  String path = "/register/dbsource";

    static {
        curatorFramework = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1")
                .sessionTimeoutMs(5000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 10)).build();
        curatorFramework.start();


    }

    @Bean
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        Stat stat = null;
        try {
            stat = curatorFramework.checkExists().forPath(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        DBProperty dbProperty=new DBProperty();
        dbProperty.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
        dbProperty.setDataSourceType("com.alibaba.druid.pool.DruidDataSource");
        dbProperty.setDriverClassName("com.mysql.jdbc.Driver");
        dbProperty.setDatasourceUserName("root");
        dbProperty.setDatasourcePassword("zsh123456");
        dbProperty.setDatasourceUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8");
        if (stat == null) {
            //持久化节点
            try {
                curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, JSON.toJSONBytes(dbProperty));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        datasource=findDataSource(dbProperty,new DruidDataSource());
        return datasource;
    }



    private DruidDataSource findDataSource(DBProperty dbProperty,DruidDataSource datasource){

        datasource.setUrl(dbProperty.getDatasourceUrl());
        datasource.setUsername(dbProperty.getDatasourceUserName());
        datasource.setPassword(dbProperty.getDatasourcePassword());
        datasource.setDriverClassName(dbProperty.getDriverClassName());
        datasource.setConnectionProperties(dbProperty.getConnectionProperties());
        return datasource;
    }

    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1")
                .sessionTimeoutMs(5000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 10)).build();
        curatorFramework.start();
         Stat stat=null;
        try {
            stat = curatorFramework.checkExists().forPath(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        DBProperty dbProperty=new DBProperty();
        dbProperty.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
        dbProperty.setDataSourceType("com.alibaba.druid.pool.DruidDataSource");
        dbProperty.setDriverClassName("com.mysql.jdbc.Driver");
        dbProperty.setDatasourceUserName("root");
        dbProperty.setDatasourcePassword("zsh123456");
        dbProperty.setDatasourceUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8");
        if (null==stat){
            curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path,JSON.toJSONBytes(dbProperty));
        }else {
            curatorFramework.setData().forPath(path,JSON.toJSONBytes(dbProperty));
        }
    }

}
